package cn.wolfcode.web.controller;

import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    // 处理查询所有部门请求
    // /permission/list 目前不带参数, 响应 HTML 格式内容
    @RequiredPermission(name="权限页面", expression = "permission:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        // 获取所有部门数据, 存到 Model 中
        model.addAttribute("pageInfo", permissionService.query(qo));
        // 找视图
        return "/permission/list"; // /WEB-INF/views/permission/list.jsp
    }

    @RequiredPermission(name="权限加载", expression = "permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        try {
            permissionService.reload();
            return new JsonResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, e.getMessage());
        }
    }

}
